package pandora.bo.DCC;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import pandora.data.IGenericSearchDAO;
import pandora.data.ISearchDAO;
import pandora.data.IStoreDAO;
import pandora.data.mysql.JDBCGenericSearchDAO;
import pandora.data.mysql.JDBCSearchDAO;
import pandora.data.mysql.JDBCStoreDAO;
import pandora.dto.ProductDTO;
import pandora.dto.StoreDTO;
import pandora.dto.TypeDTO;

public class StoreStats implements IStat{
	private StoreDTO store;
	private IGenericSearchDAO genericSearchDAO = new JDBCGenericSearchDAO();
	private ISearchDAO searchDAO = new JDBCSearchDAO();
	private IStoreDAO storeDAO = new JDBCStoreDAO();
	
	public StoreStats(String storeNIF){
		this.store = storeDAO.getStoreByNIF(storeNIF);
	}
	
	public List<ProductPar> mostSearchedProducts() {
		List<ProductPar> msp = new ArrayList<ProductPar>();
		
		List<ProductDTO> productsInStore = searchDAO.selectAllSearchedProductsAtStore(store.getStoreNIF());
		
		for(ProductDTO p : productsInStore){
			Integer c = searchDAO.countSearchedProductAtStore(store.getStoreNIF(),p.getBarcode());
			ProductPar pp = new ProductPar(p,c);
			
			if(!msp.contains(pp))
				msp.add(pp);
		}
		
		Collections.sort(msp);
		Collections.reverse(msp);
		return msp;
	}

	public List<ProductPar> mostSearchedProductsBetween(Date d1, Date d2) {
		List<ProductPar> mspb = new ArrayList<ProductPar>();
		
		List<ProductDTO> pisb = searchDAO.selectAllSearchedProductsAtStoreBetween(store.getStoreNIF(),d1, d2);
		
		for(ProductDTO p: pisb){
			Integer c = searchDAO.countSearchedProductAtStoreBetween(store.getStoreNIF(),p.getBarcode(),d1, d2);
			ProductPar pp = new ProductPar(p,c);
			
			if(!mspb.contains(pp))
				mspb.add(pp);
		}
		
		Collections.sort(mspb);
		Collections.reverse(mspb);
		return mspb;
	}

	public List<TypePar> mostSearchedTypes() {
		List<TypePar> mst = new ArrayList<TypePar>();
		
		List<TypeDTO> typesInStore = genericSearchDAO.selectAllSearchedTypesAtStore(store.getStoreNIF());
		
		for(TypeDTO t: typesInStore){
			Integer c = genericSearchDAO.countSearchedTypeAtStore(store.getStoreNIF(),t);
			TypePar tp = new TypePar(t,c);
			
			if(!mst.contains(tp))
				mst.add(tp);			
		}
		
		Collections.sort(mst);
		Collections.reverse(mst);
		return mst;
	}

	public List<TypePar> mostSearchedTypesBetween(Date d1, Date d2) {
		List<TypePar> mstb = new ArrayList<TypePar>();
		
		List<TypeDTO> tisb = genericSearchDAO.selectAllSearchedTypesAtStoreBetween(store.getStoreNIF(),d1, d2);
		
		for(TypeDTO t: tisb){
			Integer c = genericSearchDAO.countSearchedTypeAtStoreBetween(store.getStoreNIF(),t,d1, d2);
			TypePar tp = new TypePar(t,c);
			
			if(!mstb.contains(tp))
				mstb.add(tp);
		}
		
		Collections.sort(mstb);
		Collections.reverse(mstb);
		return mstb;
	}

}
